
public class SteamString {

	String value;
	SteamString next;
	private int length;
	private boolean isEmpty;

	SteamString() {
		this.value = null;
		this.next = null;
		this.length = 0;
		this.isEmpty = true;
	}

	SteamString(String c, SteamString s) {
		this.value = c;
		this.next = s;
		if (s == null) {
			this.length = 1;
		}
		else {
			this.length = s.length() + 1;
		}
		this.isEmpty = false;
	}

	public boolean hasNext() {
		return !this.next.isEmpty() ;
	}

	public boolean isEmpty() {
		return isEmpty;
	}

	public int length() {
		return this.length;
	}

	public String next() {
		if (this.hasNext()) {
			String v = this.value;
			this.value = this.next.value;
			this.length = this.next.length;
			this.isEmpty = this.next.isEmpty;
			this.next = this.next.next;
			return v;
		}
		if (!this.hasNext() && !this.isEmpty()) {
			String v = this.value;
			this.value = null;
			this.isEmpty = true;
			this.length = 0;
			return v;
		}
		if (isEmpty) {
			throw new NullPointerException();
		}
		return null;
	}

	public void add(String c) {
		if (this.isEmpty()) {
			this.value = c;
			this.length = 1;
			this.next = new SteamString();
			this.isEmpty = false;
		}
		else {
			this.next = new SteamString(this.value, this.next);
			this.value = c;
			this.length = this.length + 1;
		}
	}

	public void print() {
		if (!this.isEmpty()) {
			if (this.hasNext()) {
				System.out.println(value);
				this.next.print();
			}
			else {
				System.out.println(value);
			}
		}
	}

}
